home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / linux / tools / amiga / gzip-1.1.2.lha / gzip-1.1.2 / zip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-28  |  3.1 KB  |  119 lines

  1. /* zip.c -- compress files to the gzip or pkzip format
  2.  * Copyright (C) 1992-1993 Jean-loup Gailly
  3.  * This is free software; you can redistribute it and/or modify it under the
  4.  * terms of the GNU General Public License, see the file COPYING.
  5.  */
  6.  
  7. #ifndef lint
  8. static char rcsid[] = "$Id: zip.c,v 0.16 1993/05/28 14:51:17 jloup Exp $";
  9. #endif
  10.  
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14.  
  15. #include "tailor.h"
  16. #include "gzip.h"
  17. #include "crypt.h"
  18.  
  19. #ifdef HAVE_UNISTD_H
  20. #  include <unistd.h>
  21. #endif
  22. #ifndef NO_FCNTL_H
  23. #  include <fcntl.h>
  24. #endif
  25.  
  26. local ulg crc;       /* crc on uncompressed file data */
  27. long header_bytes;   /* number of bytes in gzip header */
  28.  
  29. /* ===========================================================================
  30.  * Deflate in to out.
  31.  * IN assertions: the input and output buffers are cleared.
  32.  *   The variables time_stamp and save_orig_name are initialized.
  33.  */
  34. int zip(in, out)
  35.     int in, out;            /* input and output file descriptors */
  36. {
  37.     uch  flags = 0;         /* general purpose bit flags */
  38.     ush  attr = 0;          /* ascii/binary flag */
  39.     ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
  40.  
  41.     ifd = in;
  42.     ofd = out;
  43.     outcnt = 0;
  44.  
  45.     /* Write the header to the gzip file. See algorithm.doc for the format */
  46.  
  47.     method = DEFLATED;
  48.     put_byte(GZIP_MAGIC[0]); /* magic header */
  49.     put_byte(GZIP_MAGIC[1]);
  50.     put_byte(DEFLATED);      /* compression method */
  51.  
  52.     if (save_orig_name) {
  53.     flags |= ORIG_NAME;
  54.     }
  55.     put_byte(flags);         /* general flags */
  56.     put_long(time_stamp);
  57.  
  58.     /* Write deflated file to zip file */
  59.     crc = updcrc(0, 0);
  60.  
  61.     bi_init(out);
  62.     ct_init(&attr, &method);
  63.     lm_init(level, &deflate_flags);
  64.  
  65.     put_byte((uch)deflate_flags); /* extra flags */
  66.     put_byte(OS_CODE);            /* OS identifier */
  67.  
  68.     if (save_orig_name) {
  69.     char *p = basename(ifname); /* Don't save the directory part. */
  70.     do {
  71.         put_char(*p);
  72.     } while (*p++);
  73.     }
  74.     header_bytes = (long)outcnt;
  75.  
  76.     (void)deflate();
  77.  
  78. #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
  79.   /* Check input size (but not in VMS -- variable record lengths mess it up)
  80.    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
  81.    */
  82.     if (ifile_size != -1L && isize != (ulg)ifile_size) {
  83.     Trace((stderr, " actual=%ld, read=%ld ", ifile_size, isize));
  84.     fprintf(stderr, "%s: %s: file size changed while zipping\n",
  85.         progname, ifname);
  86.     }
  87. #endif
  88.  
  89.     /* Write the crc and uncompressed size */
  90.     put_long(crc);
  91.     put_long(isize);
  92.     header_bytes += 2*sizeof(long);
  93.  
  94.     flush_outbuf();
  95.     return OK;
  96. }
  97.  
  98.  
  99. /* ===========================================================================
  100.  * Read a new buffer from the current input file, perform end-of-line
  101.  * translation, and update the crc and input file size.
  102.  * IN assertion: size >= 2 (for end-of-line translation)
  103.  */
  104. int file_read(buf, size)
  105.     char *buf;
  106.     unsigned size;
  107. {
  108.     unsigned len;
  109.  
  110.     Assert(insize == 0, "inbuf not empty");
  111.  
  112.     len = read(ifd, buf, size);
  113.     if (len == (unsigned)(-1) || len == 0) return (int)len;
  114.  
  115.     crc = updcrc((uch*)buf, len);
  116.     isize += (ulg)len;
  117.     return (int)len;
  118. }
  119.